package com.scenery;

import java.util.List;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;


import com.scenery.services.UserServices;
import com.scenery.util.HttpUtil;
import com.scenery.util.Loger;
import com.scenery.vo.City;
/****
 * 注册
 * 
 * */
public class Register extends Activity {
	private String tag=Register.class.getName();
	private TextView userText,pwdText,serverIp;
	private Button registBtn;
	UserServices services;
	/**
	 * @see android.app.Activity#onCreate(Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.register);
		userText=(TextView) findViewById(R.id.username);
		pwdText=(TextView) findViewById(R.id.password);
		serverIp=(EditText) findViewById(R.id.serverIp);
		registBtn=(Button)findViewById(R.id.register);
		services=new UserServices();
		registBtn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
				if(!HttpUtil.isNetWorkConnected(Register.this))
				{
					Toast.makeText(Register.this, "网络不通！", Toast.LENGTH_LONG).show();
				}else {
					RegisterAsyncTask register=new RegisterAsyncTask();
					register.init(userText.getText().toString(), pwdText.getText().toString(),serverIp.getText().toString());
					register.execute(0);
					
				}
				
			}
		});
		// TODO Put your code here
		
	}
	class RegisterAsyncTask extends AsyncTask<Integer,Integer,String>{
		ProgressDialog dialog=new ProgressDialog(Register.this);
		String user="",pwd="",ip="";
		void init(String user,String pwd,String ip)
		{
			this.user=user;
			this.pwd=pwd;
			this.ip=ip;
		}
		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			dialog.setMessage("注册中");
			dialog.show();
		}
		@Override
		protected String doInBackground(Integer... params) {
			// TODO Auto-generated method stub
			//return UserServices.register(user, pwd);
			services.setIp(ip);
			return services.registerOpenFire(user, pwd);
			
		}
		@Override
		protected void onPostExecute(String result) 
		{
			Log.d(user, "onPostExecute:"+result);
			dialog.dismiss();
			int ret=new Integer(result.trim());
			switch(ret)
			{
				case 0:
					Toast.makeText(Register.this, "成功", Toast.LENGTH_LONG).show();
					Intent intent = new Intent();
					intent.setClass(Register.this, LoginActivty.class);
					startActivity(intent);
					break;
				
				case 4:
					Toast.makeText(Register.this, "异常", Toast.LENGTH_LONG).show();
					break;
			}
		}
		
	};
}
